package de.au.fi.imp;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.au.fi.calc.CalcHelper;
import de.au.fi.domain.EntityHelper;
import de.au.fi.domain.Stock;
import de.au.fi.domain.Stock.CapType;
import de.au.fi.imp.reader.OnVistaReader;

public class OnVistaImporter {

	Stock stock;

	OnVistaReader onVistaReader;

	Logger log = LoggerFactory.getLogger(this.getClass());

	public List<Stock> importStocksFromOnVista(Stock... stocks) {
		onVistaReader = new OnVistaReader();
		for (Stock stock : stocks)
			importStockFromOnVista(stock);
		return Arrays.asList(stocks);
	}

	public void destroyReader() {
		if (onVistaReader != null)
			onVistaReader.destroy();
	}

	private Stock importStockFromOnVista(Stock stock) {
		this.stock = stock;
		try {
			String id2search = stock.getIsin();
			if (id2search == null || id2search.isEmpty())
				id2search = stock.getTicker();
			if (id2search == null || id2search.isEmpty())
				id2search = stock.getWkn();
			if (id2search == null || id2search.isEmpty()) {
				log.error("Could get data for stock {}",
						EntityHelper.log(stock));
				return null;
			}
			onVistaReader.searchForWkn(id2search);

			getBasicInfo();

			onVistaReader.openKennzahlen();

			getKennzahlenInfo();

			if (stock.getKursNow() == null || stock.getKurs1jahr() == null
					|| stock.getKurs6monate() == null) {
				onVistaReader.openCharts();
				getKurse();
			}

		} catch (Exception e) {
			log.error("Error importing wkn={}", stock.getWkn(), e);
			return null;
		}
		return stock;

	}

	private void getKennzahlenInfo() {
		String value = null;

		// yahoo now

		if (stock.getMarktKapInMio() == null) {
			value = onVistaReader.getKennzahlenMarktKap();
			if (value != null)
				stock.setMarktKapInMio(getDoubleFromString(value));
			stock.setCapTypeByMarktKap();
		}

		if (stock.getDividende() == null) {
			value = onVistaReader.getKennzahlenDividende();
			if (value != null)
				stock.setDividende(getDoubleFromString(value));
		}

		if (stock.getKgvAj() == null) {
			value = onVistaReader.getKennzahlenKgvAj();
			if (value != null)
				stock.setKgvAj(getDoubleFromString(value));
		}

		if (stock.getEpsAj() == null || stock.getEpsAj() == 0.0) {
			value = onVistaReader.getKennzahlenEpsAj();
			if (value != null)
				stock.setEpsAj(getDoubleFromString(value));
		}

		if (stock.getEpsNj() == null || stock.getEpsNj() == 0.0) {
			value = onVistaReader.getKennzahlenEpsNj();
			if (value != null)
				stock.setEpsNj(getDoubleFromString(value));
		}

		value = onVistaReader.getKennzahlenEbit();
		if (value != null)
			stock.setEbitLj(getDoubleFromString(value));

		value = onVistaReader.getKennzahlenRoe();
		if (value != null)
			stock.setRoeLj(getDoubleFromString(value));

		value = onVistaReader.getKennzahlenEigenKapQuote();
		if (value != null)
			stock.setEigKapQuLj(getDoubleFromString(value));

		value = onVistaReader.getKennzahlenKgv1j();
		if (value != null)
			stock.setKgv1j(getDoubleFromString(value));

		value = onVistaReader.getKennzahlenKgv2j();
		if (value != null)
			stock.setKgv2j(getDoubleFromString(value));

		value = onVistaReader.getKennzahlenKgv3j();
		if (value != null)
			stock.setKgv3j(getDoubleFromString(value));

		// not used in criterias yet
		// value = onVistaReader.getKennzahlenKgvNj();
		// if(value!=null)stock.set(getDoubleFromString(value));

	}

	private void getBasicInfo() {

		String value = onVistaReader.getBasicIsin();
		if (value != null)
			stock.setIsin(value);

		value = onVistaReader.getBasicBranche();
		if (value != null && value.indexOf("Banken") != -1) {
			stock.setCapType(CapType.FINANCE);
		}

		stock.setOnVistaId(onVistaReader.getOnVistaId());
	}

	private void getKurse() {
		String value = onVistaReader.getChartsKursForDateString(getDate(0));
		if (value != null)
			stock.setKursNow(getDoubleFromString(value));

		value = onVistaReader.getChartsKursForDateString(getDate(-6));
		if (value != null)
			stock.setKurs6monate(getDoubleFromString(value));

		value = onVistaReader.getChartsKursForDateString(getDate(-12));
		if (value != null)
			stock.setKurs1jahr(getDoubleFromString(value));
	}

	public static String getDate(int months) {
		Calendar cal;
		cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MONTH, months);
		if (months == 0)
			cal.add(Calendar.DAY_OF_MONTH, -1);

		SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
		return format.format(new Date(cal.getTimeInMillis())).toString();
	}

	public static Double getDoubleFromString(String value) {
		if (value == null)
			return null;
		try {
			Double d = new Double(value.replace(".", "").replace(",", ".")
					.replace("%", "").replace(" ", "").replace("Mio", "")
					.replace("EUR", ""));
			return CalcHelper.roundToTwoDigits(d);
		} catch (NumberFormatException e) {
			LoggerFactory.getLogger(OnVistaImporter.class).error("NumberformatException {}", e.getMessage());
			}
		return null;
	}
}
